package com.lfs.market.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.lfs.market.model.R;
import com.lfs.market.model.dto.UpdateUserInfoDto;
import com.lfs.market.model.dto.UserLoginDto;
import com.lfs.market.model.entity.User;
import com.lfs.market.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@SaCheckLogin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<SaTokenInfo> login(@RequestBody UserLoginDto request) {
        SaTokenInfo loginToken = userService.login(request);
        return R.ok(loginToken);
    }


    /**
     * 根据密码登录
     *
     * @param request
     * @return
     */
    @PostMapping("/login/pwd")
    public R<SaTokenInfo> loginPwd(@RequestBody UserLoginDto request) {
        SaTokenInfo loginToken = userService.loginPwd(request);
        return R.ok(loginToken);
    }

    /**
     * 注册
     *
     * @param request
     * @return
     */
    @PostMapping("/register")
    public R<SaTokenInfo> register(@RequestBody UserLoginDto request) {
        SaTokenInfo loginToken = userService.register(request);
        return R.ok(loginToken);
    }


    /**
     * 根据短信验证码登录
     *
     * @param phone
     * @return
     */
    @GetMapping("/getCheckCode")
    public R<String> getLoginCode(String phone) {
        userService.getLoginCode(phone);
        return R.ok();
    }

    @GetMapping("/logout")
    public R logout() {
        StpUtil.logout(StpUtil.getLoginId());
        return R.ok();
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    public R<User> getUserInfo() {
        return userService.getUserInfo();
    }

    @GetMapping("/getUserInfo/byId")
    public R<User> getUserInfo(String userId) {
        return userService.getUserInfo(userId);
    }

    /**
     * 修改用户信息
     *
     * @param dto
     * @return
     */
    @PutMapping
    public R updateUserInfo(@RequestBody UpdateUserInfoDto dto) {
        userService.updateUserInfo(dto);
        return R.ok();
    }

    /**
     * 修改用户密码
     *
     * @param dto
     * @return
     */
    @PutMapping("/password")
    public R updateUserInfoPass(@RequestBody UpdateUserInfoDto dto) {
        userService.updateUserInfoDetail(dto);
        return R.ok();
    }
}
